function perm(array) {
    if(array.length < 8) {
        return 0
    }
    let count = 0
    function fix(x){
        if(x === array.length-1){
            if(array[0]+array[1]+array[2]+array[3] === array[4]+array[5]+array[7]+array[6] && array[0]+array[2]+array[4]+array[6] === array[1]+array[3]+array[5]+array[7] && array[0]+array[1]+array[4]+array[5] === array[2]+array[3]+array[7]+array[6]){
                count++
            }
            return
        }else{
            let seen = new Set()
            for(let i = x;i < array.length;i++){
                if(seen.has(array[i])){
                    continue
                }
                seen.add(array[i])
                let temp = array[x]
                array[x] = array[i]
                array[i] = temp
                fix(x+1)
                temp = array[x]
                array[x] = array[i]
                array[i] = temp
            }
        }
    }
    fix(0)
    return count
}
console.log(perm([1,2,3,4,5,6,7,8]));